package com.miao.teacher.ui;

import java.math.BigDecimal;
import java.util.ArrayList;

import com.espeaker.sdk.Constants;
import com.espeaker.sdk.model.ReviseJson;
import com.google.gson.Gson;
import com.miao.teacher.app.TApp;
import com.miao.teacher.controls.LocalRevise;
import com.miao.teacher.models.ReviseInfo;
import com.miao.teacher.ui.MiaoActivity.AdvisePlay;
import com.miao.teacher.ui.MiaoActivity.CommentPlay;
import com.miao.teacher.utils.DBUtil;
import com.miao.teacher.utils.FileUtil;
import com.miao.teacher.utils.TMediaPlayer;
import com.miao.teacher.widget.SeekBarPlayer;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class HistoryDetailActivity extends BaseActivity {

	private static final String XIAOFEI_ID = "extra.xiaofei_id";

	private ReviseJson mCurReviseJson;
	private ReviseInfo rInfo;

	public static final String SOUND_TEMP_PATH = "/tofelmiao/sound/temp/";
	
	private String tempRecordFile = FileUtil.SDPATH + "/tofelmiao/sound/temp/temp_comment_{0}.amr";
	private String tempAdviseRecordFile = FileUtil.SDPATH + "/tofelmiao/sound/temp/temp_advise_{0}.amr";

	private SeekBar sBar = null;

	private TextView curTimeTv;
	private TextView totalTimeTv;
	private Button btnPlay = null;
	private Button btnPre = null;
	private Button btnNext = null;

	private SeekBarPlayer mSeekBarPlayer = null;

	AnimationDrawable playAnim = null;
	TMediaPlayer mVoicePlayer = null;
	ImageView animComment;
	ImageView animAdvise;
	/*
	 * 创建实例
	 */
	public static void createInstance(Context context, int xiaofei_id) {
		Intent intent = new Intent();
		intent.setClass(context, HistoryDetailActivity.class);
		intent.putExtra(XIAOFEI_ID, xiaofei_id);
		context.startActivity(intent);
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		try{
		mSeekBarPlayer.pause();
		mVoicePlayer.DoStop();
		}catch(Exception er)
		{}
		finish();
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_history_detail);
		if (getIntent().hasExtra(XIAOFEI_ID)) {
			int xiaofei_id = getIntent().getIntExtra(XIAOFEI_ID, 0);
			rInfo = LocalRevise
					.getByXiaoFeiId(new DBUtil(this).sld, xiaofei_id);
			if (rInfo != null) {
				Gson gson = new Gson();
				mCurReviseJson = gson.fromJson(rInfo.json, ReviseJson.class);
				if (mCurReviseJson.mFiles == null) {
					mCurReviseJson.mFiles = new ArrayList<String>();
				}
			}
		}
		
		tempRecordFile=tempRecordFile.replace("{0}", mCurReviseJson.xiaofei_id+"");
		tempAdviseRecordFile=tempAdviseRecordFile.replace("{0}", mCurReviseJson.xiaofei_id+"");
		mVoicePlayer = new TMediaPlayer();
		findViewById(R.id.lly_back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onBackPressed();
			}
		});
		setScoreDetailView();

		curTimeTv = (TextView) findViewById(R.id.tvCurTime);
		totalTimeTv = (TextView) findViewById(R.id.tvTotalTime);
		sBar = (SeekBar) findViewById(R.id.sBar);
		btnPlay = (Button) findViewById(R.id.btnPlayerPlay);
		btnPre = (Button) findViewById(R.id.btnPlayerPre);
		btnNext = (Button) findViewById(R.id.btnPlayerNext);
		btnPlay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mSeekBarPlayer != null) {
					if (mSeekBarPlayer.isReady()) {
						if (mSeekBarPlayer.isPlaying()) {
							mSeekBarPlayer.pause();
							arg0
									.setBackgroundResource(R.drawable.iv_audio_play);
						} else {
							if (playAnim != null) {
								playAnim.stop();
							}
							mVoicePlayer.DoStop();
							mSeekBarPlayer.play();
							arg0
									.setBackgroundResource(R.drawable.iv_audio_pause);
						}
					}

				} else {
					arg0.setBackgroundResource(R.drawable.iv_audio_pause);
				}
			}
		});
		btnPre.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mSeekBarPlayer == null)
					return;
				if (mSeekBarPlayer.isReady()) {
					mSeekBarPlayer.backward();
				}
			}
		});
		btnNext.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mSeekBarPlayer == null)
					return;
				if (mSeekBarPlayer.isReady()) {
					mSeekBarPlayer.forward();
				}
			}
		});
		SeekBarPlayer.setHandler(mHandler);
		mSeekBarPlayer = SeekBarPlayer.create(FileUtil.SDPATH + SOUND_TEMP_PATH
				+ mCurReviseJson.xiaofei_id + "_revise.amr", sBar, curTimeTv,
				totalTimeTv);
		if (mSeekBarPlayer != null) {
			mSeekBarPlayer.setOnCompletionListener(new OnCompletionListener() {
				@Override
				public void onCompletion(MediaPlayer arg0) {
					sBar.setThumb(HistoryDetailActivity.this.getResources()
							.getDrawable(R.drawable.seek_thumb_play));
					mSeekBarPlayer.seekTo(0);
					btnPlay.setBackgroundResource(R.drawable.iv_audio_play);
				}
			});
		}

		final LinearLayout llyAudio = (LinearLayout) findViewById(R.id.lly_audio_comment);
		final LinearLayout llyAudioAdvise = (LinearLayout)findViewById(R.id.lly_audio_advise);
		animComment = (ImageView)findViewById(R.id.imgAudio_comment);
		animAdvise = (ImageView)findViewById(R.id.imgAudio_advise);
		llyAudio.setOnClickListener(new CommentPlay());
		llyAudioAdvise.setOnClickListener(new AdvisePlay());
		
		((TextView) findViewById(R.id.tvTime1)).setText(mVoicePlayer.getLength(tempRecordFile)/1000+"'");
		((TextView) findViewById(R.id.tvTime2)).setText(mVoicePlayer.getLength(tempAdviseRecordFile)/1000+"'");
		
		findViewById(R.id.tvQuestion).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				QuestionViewActivity.createInstance(
						HistoryDetailActivity.this, mCurReviseJson.title);
			}
		});
		((TextView) findViewById(R.id.tv_topbar_middle)).setText(mCurReviseJson.title);
	}
	@Override
	protected void RecMsgFromHandler(Message msg) {
		// TODO Auto-generated method stub
		super.RecMsgFromHandler(msg);
		switch (msg.what) {
		case TApp.GOLDACTIVITY_PLAYFINISH:
			if (playAnim != null) {
				playAnim.stop();
			}
			break;
		}
	}
	/*
	 * 播放评论录音
	 */
	class CommentPlay implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try {
				mVoicePlayer.ASycPlay(tempRecordFile, mHandler);
				animComment
						.setBackgroundResource(R.drawable.anim_revise_playing);
				if (playAnim != null) {
					playAnim.stop();
				}
				playAnim = (AnimationDrawable) animComment.getBackground();
				playAnim.start();
				if (mSeekBarPlayer.isPlaying()) {
					btnPlay.performClick();
				}
			} catch (Exception er) {
			}
		}

	}

	/*
	 * 播放建议录音
	 */
	class AdvisePlay implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try {
				mVoicePlayer.ASycPlay(tempAdviseRecordFile, mHandler);
				animAdvise
						.setBackgroundResource(R.drawable.anim_revise_playing);
				if (playAnim != null) {
					playAnim.stop();
				}
				playAnim = (AnimationDrawable) animAdvise.getBackground();
				playAnim.start();
				if (mSeekBarPlayer.isPlaying()) {
					btnPlay.performClick();
				}
			} catch (Exception er) {
			}
		}

	}
	private void setScoreDetailView() {
		if (mCurReviseJson.delivery_score >= 1) {
			((ImageView) findViewById(R.id.img_delivery_star1))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.iv_myrevisers_star_green));
		}
		if (mCurReviseJson.delivery_score >= 2) {
			((ImageView) findViewById(R.id.img_delivery_star2))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.iv_myrevisers_star_green));
		}
		if (mCurReviseJson.delivery_score >= 3) {
			((ImageView) findViewById(R.id.img_delivery_star3))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.iv_myrevisers_star_green));
		}
		if (mCurReviseJson.delivery_score >= 4) {
			((ImageView) findViewById(R.id.img_delivery_star4))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.iv_myrevisers_star_green));
		}

		if (mCurReviseJson.language_score >= 1) {
			((ImageView) findViewById(R.id.img_lau_star1))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.iv_myrevisers_star_green));
		}
		if (mCurReviseJson.language_score >= 2) {
			((ImageView) findViewById(R.id.img_lau_star2))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.iv_myrevisers_star_green));
		}
		if (mCurReviseJson.language_score >= 3) {
			((ImageView) findViewById(R.id.img_lau_star3))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.iv_myrevisers_star_green));
		}
		if (mCurReviseJson.language_score >= 4) {
			((ImageView) findViewById(R.id.img_lau_star4))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.iv_myrevisers_star_green));
		}

		if (mCurReviseJson.topic_score >= 1) {
			((ImageView) findViewById(R.id.img_tdev_star1))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.iv_myrevisers_star_green));
		}
		if (mCurReviseJson.topic_score >= 2) {
			((ImageView) findViewById(R.id.img_tdev_star2))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.iv_myrevisers_star_green));
		}
		if (mCurReviseJson.topic_score >= 3) {
			((ImageView) findViewById(R.id.img_tdev_star3))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.iv_myrevisers_star_green));
		}
		if (mCurReviseJson.topic_score >= 4) {
			((ImageView) findViewById(R.id.img_tdev_star4))
					.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.iv_myrevisers_star_green));
		}
		float total = mCurReviseJson.delivery_score
				+ mCurReviseJson.language_score + mCurReviseJson.topic_score;
		double d = 0;
		if ((total / 3 * 7.5) % ((int) (total / 3 * 7.5)) == 0.25) {
			d = total / 3 * 7.5 + 0.25;
		} else {
			d = total / 3 * 7.5;
		}
		((TextView) findViewById(R.id.img_score)).setText(new BigDecimal(d).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue() + "'");
	}
}
